﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpGL;

namespace bitzhuwei.SharpGLController
{
    public partial class GetFloatConrol : UserControl
    {
        private OpenGL gl;
        public GetFloatConrol()
        {
            InitializeComponent();
        }
        public GetFloatConrol(OpenGL gl)
        {
            InitializeComponent();
            this.SetOpenGL(gl);
        }

        void Application_Idle(object sender, EventArgs e)
        {
            if (gl != null)
            {
                var targetType = this.comboBox1.SelectedItem;
                if (targetType != null)
                {
                    var eType = (SharpGL.Enumerations.GetTarget)Enum.Parse(
                        typeof(SharpGL.Enumerations.GetTarget), targetType.ToString());
                    var target = new float[100];
                    var initialFloat = new float();
                    gl.GetFloat(eType, target);
                    var index = target.Length - 1;
                    while (index >= 0 && target[index] <= initialFloat)
                    {
                        index--;
                    }
                    var builder = new StringBuilder();
                    for (int i = 0; i < index + 1; i++)
                    {
                        builder.Append(target[i]);
                        var interger = (int)(target[i]);
                        if (((float)interger) == target[i])
                        {
                            builder.Append("(0x");
                            builder.Append(Convert.ToString(interger, 16));
                            builder.Append(")");
                        }
                        builder.Append(Environment.NewLine);
                    }
                    this.textBox1.Text = builder.ToString();
                    //throw new NotImplementedException();
                }
            }
        }

        string[] targetNames = null;
        private void GetFloatConrol_Load(object sender, EventArgs e)
        {
            if (this.targetNames == null)
                this.targetNames = Enum.GetNames(typeof(SharpGL.Enumerations.GetTarget));
            for (int i = 0; i < this.targetNames.Length; i++)
            {
                var p = i;
                for (int j = i + 1; j < this.targetNames.Length; j++)
                {
                    if (this.targetNames[p].CompareTo(this.targetNames[j])>0)
                        p = j;
                }
                if (p != i)
                {
                    var tmp = this.targetNames[p];
                    this.targetNames[p] = this.targetNames[i];
                    this.targetNames[i] = tmp;
                }
            }

            this.comboBox1.Items.Clear();
            foreach (var item in this.targetNames)
            {
                this.comboBox1.Items.Add(item);
            }
        }
        EventHandler appIdleHandler = null;
        internal void SetOpenGL(OpenGL gl)
        {
            this.gl = gl;
            if (this.gl != null && appIdleHandler == null)
            {
                appIdleHandler = new EventHandler(Application_Idle);
                Application.Idle += appIdleHandler;
            }
        }
    }
}
